<template>
  <div class="uploadImg">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img
        v-if="imageUrl"
        v-imgError="defaultImg"
        :src="imageUrl"
        class="avatar"
        @click.stop="showBigImage"
      >
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <!-- 关闭x -->
    <div v-if="imageUrl" class="close" @click="closeEvent">X</div>
    <!-- 图片预览 -->
    <el-dialog title="图片预览" :visible.sync="isShow">
      <div style="text-align: center">
        <img :src="imageUrl" class="img">
      </div>
    </el-dialog>
    <!-- 进度条 -->
    <transition name="xxx">
      <el-progress v-if="showProgress" :percentage="percentage" />
    </transition>
  </div>
</template>

<script>
//  腾讯云上传使用
//   1：安装插件
//      cos-js-sdk-v5   yarn add  cos-js-sdk-v5
//   2:导入
//     import Cos from 'cos-js-sdk-v5'
//   3:实例化
//   let cos = new Cos({
//     SecretId: 'COS_SECRETID', // 身份识别 ID
//     SecretKey: 'COS_SECRETKEY', // 身份密钥
// });
//   4:使用
//   cos.putObject({
//     Bucket: 'dhf481229-1305249343', /* 存储桶: */
//     Region: 'ap-guangzhou',     /* 存储桶所在地域，必须字段 */
//     Key: uid+'',              /* 必须是字符串 */
//     StorageClass: 'STANDARD',  // 固定值
//     Body: 文件对象, // 上传文件对象
//     onProgress: (progressData)=> {
//         上传进度
//         console.log(JSON.stringify(progressData));
//     }
// }, (err, data)=> {
//   // 上传成功或者失败的回调
//     console.log(err || data);
// });
import Cos from 'cos-js-sdk-v5'
const cos = new Cos({
  SecretId: 'AKIDe6JHzPZS4OSFl11kCNIxLSHq89tMYT7U', // 身份识别 ID
  SecretKey: '7xWcS1II2VgaDbkcDYcPvBAsIRJtCEzT' // 身份密钥
})
export default {
  name: 'UpLoadImg',
  model: {
    prop: 'imageUrl'
  },
  props: {
    imageUrl: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      percentage: 0,
      isShow: false,
      showProgress: false,
      defaultImg: require('@/assets/common/head.jpg')
    }
  },
  methods: {
    // handleAvatarSuccess(res, file) {
    //   this.imageUrl = URL.createObjectURL(file.raw)
    // },
    // 上传前 控制图片类型和大小
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/png'
      const isLt1M = file.size / 1024 / 1024 < 1

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt1M) {
        this.$message.error('上传头像图片大小不能超过 1MB!')
      }
      return isJPG && isLt1M
    },
    // 自定义上传
    httpRequest(res) {
      this.showProgress = true
      this.percentage = 0
      console.log(res, 1111)
      cos.putObject(
        {
          Bucket: 'hr-1308460166' /* 存储桶: */,
          Region: 'ap-nanjing' /* 存储桶所在地域，必须字段 */,
          Key: res.file.uid + '' /* 必须是字符串 */,
          StorageClass: 'STANDARD', // 固定值
          Body: res.file, // 上传文件对象
          onProgress: (progressData) => {
            // 上传进度
            this.percentage = progressData.percent * 100
            if (progressData.percent === 1) {
              setTimeout(() => {
                this.showProgress = false
              }, 1000)
            }
            console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          // 上传成功或者失败的回调]
          console.log(err || data)
          // console.log(this, 'this')
          // this.imageUrl = 'http://' + data.Location
          this.$emit('input', 'http://' + data.Location)
        }
      )
    },
    // 点击x
    closeEvent() {
      this.$emit('input', '')
    },
    // 大图预览
    showBigImage() {
      this.isShow = true
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.uploadImg {
  position: relative;
  width: 178px;
  &:hover {
    .close {
      display: block;
    }
  }
  .close {
    display: none;
    position: absolute;
    top: 5px;
    right: 5px;
    cursor: pointer;
  }
  .img {
    width: 100%;
  }
}
</style>
